/**
 *	jslib base information
 */
jslib = {
	$path : 'jslib',
	version : '1.0',
	module : {$path : 'jslib.module'},
	plugin : {$path : 'jslib.plugin'},
	runtime : {$path : 'jslib.runtime'}
};

/**
 *	using namespace method
 *  @param 		path			build nameSpace
 *  @param		isJustSearch	for Search namespace or class
 *	@return		m				namespace
 */
jslib.$us = function (path, isJustSearch) {
	path = (path || '').split(/\s*\.\s*/g);
	
	var m = window, d, ns;
	for(var i = 0, l = path.length; i < l; i++){
		ns = path[i];
		
		if(d) d += '.' + ns;
		else d = ns;
		
		if(!m[ns]){
			if(isJustSearch) 
				return null;
			else 
				m[ns] = {$path : d};
		}
		
		m = m[ns];
	}
	
	return m;
};

/**
 *	extend class function
 *  @param 		original	child class
 *  @param 		extended 	father object or function
 *  @return		extended child	
 */
jslib.$et = function (original, extended){
	for (var key in (extended || {})){
		if(typeof extended[key] === 'object')
			original[key] = jslib.$et({}, extended[key])
		else
			original[key] = extended[key];
	}
	return original;
}; 

/**
 *	regist class function
 *  @param 		name	className
 *  @param 		obj 	init param Object
 *  @param 		dep 	depender object
 *  @return		null	
 */
jslib.$_$ = function (name,obj,dep){
	//define 
	var namespace = '', 
		clsname = '', 
		cls = function(){};
	//check depender class
	if(dep){
		if(typeof dep !== 'Array')return;
		for(var item in dep){
			if(!jslib.$us(item,true)){
				(console ? console.log : window.alert)('depender ' + item + ' not exist!');
				return;
			}
		}
	}
	//build namespace and classname
	try{
		var arr = name.split('.');
		for(var i = 0 ; i < arr.length ; i++ ){
			if(i == arr.length - 1){
				clsname = arr[i];
			}
			else{
				namespace += '.' + arr[i]; 
			}
		}
		namespace = namespace.slice(1);
	}catch(ex){}

	jslib.$et(cls.prototype, obj);

	/**
	 *	getObject function
	 *  @param 		options	init params
	 *  @return		object	
	 */
	cls.getObject = function(options){
		var x = {};
		jslib.$et(x, cls.prototype);
		jslib.$et(x.options, options);
		
		if(x.initialize){
				x.initialize();
				if(x.options.thisname)
					jslib.runtime[x.options.thisname] = x;
				delete x.initialize;
		}
		
		return x;
	}
	
	jslib.$us(namespace)[clsname] = cls;
};
